package com.company.project.modular.caigou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.modular.system.service.ISupplierService;
import com.company.project.modular.system.model.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 供应商管理控制器
 *
 * @author fengshuonan
 * @Date 2018-10-30 15:33:28
 */
@Controller
@RequestMapping("/supplier")
public class SupplierController extends BaseController {

    private String PREFIX = "/caigou/supplier/";

    @Reference
    private ISupplierService supplierService;

    /**
     * 跳转到供应商管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "supplier.html";
    }

    /**
     * 跳转到添加供应商管理
     */
    @RequestMapping("/supplier_add")
    public String supplierAdd() {
        return PREFIX + "supplier_add.html";
    }

    /**
     * 跳转到修改供应商管理
     */
    @RequestMapping("/supplier_update/{supplierId}")
    public String supplierUpdate(@PathVariable Integer supplierId, Model model) {
        Supplier supplier = supplierService.selectById(supplierId);
        model.addAttribute("item",supplier);
        LogObjectHolder.me().set(supplier);
        return PREFIX + "supplier_edit.html";
    }

    /**
     * 获取供应商管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return supplierService.selectList(null);
    }

    /**
     * 新增供应商管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Supplier supplier,BindingResult bindingResult) {
        supplierService.insert(supplier);
        return SUCCESS_TIP;
    }

    /**
     * 删除供应商管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer supplierId) {
        supplierService.deleteById(supplierId);
        return SUCCESS_TIP;
    }

    /**
     * 修改供应商管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Supplier supplier,BindingResult bindingResult) {
        supplierService.updateById(supplier);
        return SUCCESS_TIP;
    }

    /**
     * 供应商管理详情
     */
    @RequestMapping(value = "/detail/{supplierId}")
    @ResponseBody
    public Object detail(@PathVariable("supplierId") Integer supplierId) {
        return supplierService.selectById(supplierId);
    }
}
